<template>
  <div class="tank_status">
    <!-- tankStatusCount 组件 -->
    <div class="tank_status_header">
      <tankStatusCount
        :empty="tankStatusCountVo.empty"
        :premix="tankStatusCountVo.premix"
        :aftermix="tankStatusCountVo.aftermix"
        :transfer="tankStatusCountVo.transfer"
        :yichang="tankStatusCountVo.yichang"
        :pack="tankStatusCountVo.pack"
        :qa="tankStatusCountVo.qa"
      ></tankStatusCount>
    </div>

    <el-row>
      <el-col
        :span="12"
        style="padding-right: 20px; border-right: 1px solid #ccc"
      >
        <div style="display: flex">
          <span class="lantiao"></span>
          <span class="tit">Platform 1</span>
        </div>
        <!-- tank-status-card 组件列表 -->
        <div class="tank_status_card_container">
          <tank-status-card
            v-for="(tankStatusCardVo, index) in cardList"
            :key="index"
            :productionTime="tankStatusCardVo.productionTime"
            :deviceName="tankStatusCardVo.deviceName"
            :productName="tankStatusCardVo.productName"
            :orderNumber="tankStatusCardVo.orderNumber"
            :operator="tankStatusCardVo.operator"
            :color="tankStatusCardVo.color"
          ></tank-status-card>
        </div>
      </el-col>
      <el-col
        :span="12"
        style="padding-left: 20px; border-left: 1px solid #ccc"
      >
        <div style="display: flex">
          <span class="lantiao"></span>
          <span class="tit">Platform 2</span>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script setup name="dataViews" lang="ts">
import { onMounted, reactive, Ref, ref, onUnmounted } from "vue";
import tankStatusCount from "@/components/TankStatus/tankStatusCount.vue";
import tankStatusCard from "@/components/TankStatus/index.vue";
import { getTankStatusCardData, getTankStatusCountData } from "@/api/home";
interface TankStatusCount {
  empty: number;
  premix: number;
  aftermix: number;
  transfer: number;
  yichang: number;
  pack: number;
  qa: number;
}
interface TankStatusCard {
  productionTime: string;
  deviceName: string;
  productName: string;
  orderNumber: string;
  operator: string;
  color: string;
}

const tankStatusCountVo = ref<TankStatusCount>({
  empty: 0,
  premix: 0,
  aftermix: 0,
  transfer: 0,
  yichang: 0,
  pack: 0,
  qa: 0,
});
const cardList = ref<TankStatusCard[]>([]);

let intervalTimer: number | null = null;
let abortController: AbortController | null = null;

onMounted(() => {
  const fetchData = async () => {
    // 取消上一个未完成的请求
    if (abortController) {
      abortController.abort();
    }
    abortController = new AbortController();

    try {
      await getTankStatusCount({ signal: abortController.signal });
      await getTankStatusCard({ signal: abortController.signal });
    } catch (error) {
      if (error.name !== "AbortError") {
        console.error("请求失败:", error);
      }
    }
  };

  fetchData();
  intervalTimer = setInterval(fetchData, 10_000);
});

onUnmounted(() => {
  if (intervalTimer) clearInterval(intervalTimer);
  if (abortController) abortController.abort();
});
const getTankStatusCount = async () => {
  getTankStatusCountData().then((res: any) => {
    if (res.code == 200) {
      console.log("res.data" + JSON.stringify(res.data));
      tankStatusCountVo.value = res.data as TankStatusCount;
      console.log(
        "tankStatusCountVo" + JSON.stringify(tankStatusCountVo.value)
      );
    }
  });
};
const getTankStatusCard = async () => {
  getTankStatusCardData().then((res: any) => {
    if (res.code == 200) {
      cardList.value = res.data;
    }
  });
};
</script>

<style scoped lang="scss">
.tank_status {
  display: flex;
  flex-direction: column; // 改为垂直排列
  padding: 20px;
  width: 100%;
  min-height: 60vh; // 使用视口高度单位
  gap: 20px; // 添加组件间距
  background-color: #93aec5;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
  overflow: hidden; /* 确保不会有内容溢出 */
}

.tank_status_header {
  width: 100%;
  height: auto; // 根据内容自动调整高度
  background-color: #93aec5;
  margin-bottom: 25px;
}
.tank_status_card_container {
  display: flex;
  flex-wrap: wrap; /* 允许子元素换行 */
  gap: 10px; /* 子元素之间的间距，可以根据需要调整 */
  overflow-y: auto; // 添加垂直滚动条（可选）
  // margin-left: 60px;
}

.tank-status-card {
  width: 100%; // 卡片占满容器宽度
  flex: 0 0 calc(12.5% - 10px); /* 每个子元素占据的宽度，25%是因为要每行4个，减去的10px是间距的一半 */
  box-sizing: border-box; /* 确保padding和border不会增加元素的宽度 */
  /* 其他样式可以根据需要添加 */
}

.lantiao {
  display: inline-block;
  width: 10px;
  height: 20px;
  background-color: #424ed2;
}
.tit {
  height: 20px;
  line-height: 20px;
  margin-left: 10px;
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>
